package com.vote.web.controller.business;

import com.vote.business.domain.VoteOption;
import com.vote.business.domain.vo.VoteOptionWithTitleVO;
import com.vote.business.service.IVoteOptionService;
import com.vote.common.annotation.Log;
import com.vote.common.core.controller.BaseController;
import com.vote.common.core.domain.AjaxResult;
import com.vote.common.core.page.TableDataInfo;
import com.vote.common.enums.BusinessType;
import com.vote.common.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 投票选项Controller
 * 
 * @author ruoyi
 * @date 2025-07-14
 */
@RestController
@RequestMapping("/business/voteOption")
public class VoteOptionController extends BaseController
{
    @Autowired
    private IVoteOptionService voteOptionService;

    /**
     * 查询投票选项列表
     */
    @PreAuthorize("@ss.hasPermi('business:voteOption:list')")
    @GetMapping("/list")
    public TableDataInfo list(VoteOption voteOption)
    {
        startPage();
        List<VoteOption> list = voteOptionService.selectVoteOptionList(voteOption);
        return getDataTable(list);
    }

    /**
     * 查询投票选项列表
     */
    @PreAuthorize("@ss.hasPermi('business:voteOption:list')")
    @GetMapping("/listWithTitle")
    public TableDataInfo listWithTitle(VoteOptionWithTitleVO voteOptionWithTitleDTO)
    {
        startPage();
        List<VoteOptionWithTitleVO> list = voteOptionService.selectVoteOptionWithTitleDTOList(voteOptionWithTitleDTO);
        return getDataTable(list);
    }

    /**
     * 导出投票选项列表
     */
    @PreAuthorize("@ss.hasPermi('business:voteOption:export')")
    @Log(title = "投票选项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VoteOption voteOption)
    {
        List<VoteOption> list = voteOptionService.selectVoteOptionList(voteOption);
        ExcelUtil<VoteOption> util = new ExcelUtil<VoteOption>(VoteOption.class);
        util.exportExcel(response, list, "投票选项数据");
    }

    /**
     * 获取投票选项详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:voteOption:query')")
    @GetMapping(value = "/{optionId}")
    public AjaxResult getInfo(@PathVariable("optionId") Long optionId)
    {
        return success(voteOptionService.selectVoteOptionByOptionId(optionId));
    }

    /**
     * 新增投票选项
     */
    @PreAuthorize("@ss.hasPermi('business:voteOption:add')")
    @Log(title = "投票选项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VoteOption voteOption)
    {
        return toAjax(voteOptionService.insertVoteOption(voteOption));
    }

    /**
     * 修改投票选项
     */
    @PreAuthorize("@ss.hasPermi('business:voteOption:edit')")
    @Log(title = "投票选项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VoteOption voteOption)
    {
        // 检查投票状态，只有草稿和发布状态可以修改选项
        if (!voteOptionService.checkVoteStatusForModify(voteOption.getVoteId())) {
            return AjaxResult.error("只有草稿和发布状态的投票可以修改选项");
        }
        return toAjax(voteOptionService.updateVoteOption(voteOption));
    }

    /**
     * 删除投票选项
     */
    @PreAuthorize("@ss.hasPermi('business:voteOption:remove')")
    @Log(title = "投票选项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{optionIds}")
    public AjaxResult remove(@PathVariable Long[] optionIds)
    {
        // 检查投票状态，只有草稿状态可以删除选项
        if (!voteOptionService.checkVoteStatusForDelete(optionIds)) {
            return AjaxResult.error("只有草稿状态的投票可以删除选项");
        }
        return toAjax(voteOptionService.deleteVoteOptionByOptionIds(optionIds));
    }
}
